/**
 * 配置文件
 * 管理API基础URL、常量配置等
 */

// 环境配置
const ENV = {
  // 开发环境
  development: {
    apiBaseURL: 'https://dev-api.your-domain.com/api',
    wsBaseURL: 'wss://dev-api.your-domain.com/ws',
    mqttBroker: 'wss://dev-mqtt.your-domain.com:8084/mqtt'
  },
  
  // 测试环境
  testing: {
    apiBaseURL: 'https://test-api.your-domain.com/api',
    wsBaseURL: 'wss://test-api.your-domain.com/ws',
    mqttBroker: 'wss://test-mqtt.your-domain.com:8084/mqtt'
  },
  
  // 生产环境
  production: {
    apiBaseURL: 'https://api.your-domain.com/api',
    wsBaseURL: 'wss://api.your-domain.com/ws',
    mqttBroker: 'wss://mqtt.your-domain.com:8084/mqtt'
  }
}

// 当前环境（根据需要修改）
const CURRENT_ENV = 'development'

// 当前环境配置
const currentConfig = ENV[CURRENT_ENV]

// 应用配置
const config = {
  // API配置
  api: {
    baseURL: currentConfig.apiBaseURL,
    timeout: 30000,
    retryTimes: 3,
    retryDelay: 1000
  },
  
  // WebSocket配置
  websocket: {
    baseURL: currentConfig.wsBaseURL,
    reconnectInterval: 5000,
    maxReconnectTimes: 5,
    heartbeatInterval: 30000
  },
  
  // MQTT配置
  mqtt: {
    broker: currentConfig.mqttBroker,
    clientIdPrefix: 'miniprogram_',
    keepAlive: 60,
    connectTimeout: 30000,
    reconnectPeriod: 1000,
    qos: 1,
    topics: {
      alarm: 'school/alarm/data',
      device: 'school/device/status',
      command: 'school/command'
    }
  },
  
  // 文件上传配置
  upload: {
    maxFileSize: 50 * 1024 * 1024, // 50MB
    allowedImageTypes: ['jpg', 'jpeg', 'png', 'webp'],
    allowedVideoTypes: ['mp4', 'mov', '3gp'],
    allowedAudioTypes: ['mp3', 'wav', 'aac'],
    maxImageSize: 10 * 1024 * 1024, // 10MB
    maxVideoSize: 50 * 1024 * 1024, // 50MB
    maxAudioSize: 20 * 1024 * 1024, // 20MB
    imageQuality: 0.8,
    videoMaxDuration: 60 // 秒
  },
  
  // 存储键名
  storageKeys: {
    accessToken: 'access_token',
    refreshToken: 'refresh_token',
    userInfo: 'user_info',
    appSettings: 'app_settings',
    alarmRecords: 'alarm_records',
    deviceSettings: 'device_settings'
  },
  
  // 报警配置
  alarm: {
    // 报警类型
    types: {
      verbal: 'verbal_bullying',
      physical: 'physical_bullying',
      social: 'social_bullying',
      cyber: 'cyber_bullying',
      emergency: 'emergency',
      suspicious: 'suspicious'
    },
    
    // 报警级别
    levels: {
      low: 'low',
      medium: 'medium',
      high: 'high',
      critical: 'critical'
    },
    
    // 自动报警阈值
    thresholds: {
      soundLevel: 70, // 分贝
      confidence: 0.7, // 置信度
      duration: 3000 // 持续时间(毫秒)
    }
  },
  
  // 定位配置
  location: {
    enableHighAccuracy: true,
    timeout: 15000,
    maximumAge: 300000, // 5分钟
    geocodingProvider: 'tencent' // 腾讯地图API
  },
  
  // 界面配置
  ui: {
    // 主题色
    theme: {
      primary: '#4FC3F7',
      success: '#52c41a',
      warning: '#faad14',
      error: '#ff4d4f',
      info: '#1890ff'
    },
    
    // 动画配置
    animation: {
      duration: 300,
      timingFunction: 'ease-out'
    },
    
    // 分页配置
    pagination: {
      pageSize: 20,
      maxPage: 100
    }
  },
  
  // 调试配置
  debug: {
    enabled: CURRENT_ENV !== 'production',
    logLevel: CURRENT_ENV === 'production' ? 'error' : 'debug',
    mockData: CURRENT_ENV === 'development',
    showPerformance: CURRENT_ENV !== 'production'
  }
}

// 获取配置
function getConfig(key) {
  if (!key) return config
  
  const keys = key.split('.')
  let result = config
  
  for (const k of keys) {
    if (result && typeof result === 'object' && result.hasOwnProperty(k)) {
      result = result[k]
    } else {
      return undefined
    }
  }
  
  return result
}

// 设置配置（仅允许某些配置项修改）
function setConfig(key, value) {
  const allowedKeys = [
    'api.baseURL',
    'websocket.baseURL',
    'mqtt.broker',
    'debug.enabled',
    'debug.logLevel'
  ]
  
  if (!allowedKeys.includes(key)) {
    console.warn(`配置项 ${key} 不允许动态修改`)
    return false
  }
  
  const keys = key.split('.')
  let target = config
  
  for (let i = 0; i < keys.length - 1; i++) {
    if (!target[keys[i]] || typeof target[keys[i]] !== 'object') {
      target[keys[i]] = {}
    }
    target = target[keys[i]]
  }
  
  target[keys[keys.length - 1]] = value
  return true
}

// 环境检测
function isDevelopment() {
  return CURRENT_ENV === 'development'
}

function isProduction() {
  return CURRENT_ENV === 'production'
}

function isTesting() {
  return CURRENT_ENV === 'testing'
}

// 导出配置
module.exports = {
  config,
  getConfig,
  setConfig,
  isDevelopment,
  isProduction,
  isTesting,
  CURRENT_ENV
} 